import axios from "axios";
import router from "../router/index.js";
import {ElMessage} from "element-plus";
//请求默认值
export let baseURL = "http://localhost:9527";
// export let baseURL = "http://119.45.124.76:9527";
axios.defaults.baseURL = baseURL;

// 封装get请求
export function get(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: data
        }).then((res) => {
            resolve(res);
        }, (err) => {
            reject(err)
        })
    })
}
//封装post请求
export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data).then(
            (res) => {
                resolve(res);
            },
            (err) => {
                reject(err);
            }
        );
    });
}

//请求拦截
axios.interceptors.request.use((req) => {
    if(req.url=="/login"||req.url == "/addError" || req.url=="/signadd"){
        return req
    }else{
         let token = JSON.parse(sessionStorage.getItem("userinfo")).token;
         req.headers.token = token;
    }
    console.log(req, "这是你的请求信息");
    return req;
}, (err) => {
    console.log(err, "这是你的请求信息报错");
    return Promise.reject(err);
})
//相应拦截
axios.interceptors.response.use((res) => {
    console.log(res.data, "这是你的响应信息");

    if(res.data.code){
        router.push("/login");
        ElMessage.warning("登录信息已失效、请重新登录！");
    }
    return res;
}, (err) => {
    console.log(err, "这是你的响应信息报错");
    return Promise.reject(err);
})

export const Api = {
    // 登录
    login: (props) => {
        return get("/login", props);
    },
    //更新用户登录信息
    userChage: (props) => {
        return post("/userChage", props);
    },
    // 记录日志信息
    log: (props) => {
        return post("/log", props)
    },
    // 获取日志信息
    getlog: (props) => {
        return get("/getlog", props)
    },
    // 查询用户
    userFind: (props) => {
        return get("/userFind", props)
    },
    //注册
    signAdd: (props) => {
        return post("/signadd", props)
    },
    //删除用户
    removeUser: (props) => {
        return post("/removeUser", props)
    },
    //修改用户积分信息
    integralChange: (props) => {
        return post("/integralChange", props)
    },
    //修改用户信息
    datumChange: (props) => {
        return post("/datumChange", props)
    },
    //查询用户所有数据
    getData: (props) => {
        return get("/getData", props)
    },
    //添加数据
    addData: (props) => {
        return post("/add", props)
    },
    //修改数据
    dataChange: (props) => {
        return post("/change", props)
    },
    //删除数据
    dataRemove: (props) => {
        return post("/remove", props)
    },
    //更改用户权限
    changeControl: (props) => {
        return post("/changeControl", props)
    },
    //添加消费记录
    expenseAdd: (props) => {
        return post("/expenseAdd", props)
    },
    //查询消费数据分列表
    getexpense: (props) => {
        return get("/getexpense", props)
    },
    //发送邮件
    sendEmail: (props) => {
        return post("/addSystem", props)
    },
    //获取邮件列表
    getSysList: (props) => {
        return get("/getSysList", props)
    },
    //改变邮件状态
    changeMsg: (props) => {
        return post("/changeMsg", props)
    },
    //获取相册列表
    getImage: (props) => {
        return get("/getImage", props)
    },
    //添加结缘
    addLove: (props) => {
        return post("/addLove", props)
    },
    //获取结缘信息
    getLove: (props) => {
        return get("/getLove", props)
    },
    //删除结缘信息
    delLove: (props) => {
        return get("/delLove", props)
    },
    //修改结缘信息
    changeLove: (props) => {
        return get("/changeLove", props)
    },
    //删除token
    deleteToken:(props)=>{
        return get("/deleteToken",props)
    },
    //捕获异常
    addError:(props)=>{
        return post("/addError",props)
    },
}